// /////////////////////////////////////////////////////////////////////////////
//
// Name:            asEntityComponent.h
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Binds the EntityComponent class to AngelScript
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __ASENTITYCOMPONENT_H__
#define __ASENTITYCOMPONENT_H__

// Include files
#include "../../dependencies.h"

#ifdef __COMPILE_WITH_ANGELSCRIPT__

#include "asSerializable.h"

#include "../../core/EntityComponent.h"
#include "../ScriptHelper.h"


// ScriptedGameState class
//! A class derived from EntityComponent, which makes it possible to interact with classes derived
//! from EntityComponent within AngelScript.
//! @note For internal use only!
class ScriptedEntityComponent : public EntityComponent
{
public:

    // Initialisation, deinitialisation and control...
    ScriptedEntityComponent(asIScriptObject *object);
    virtual ~ScriptedEntityComponent();

private:

    // Private members
    asIScriptObject *mObject;
    asIScriptContext *mContext;

};

//! Binds the EntityComponent class to AngelScript.
extern void bindEntityComponent(asIScriptEngine *engine);

//! Binds the base behaviours and methods of the EntityComponent to the given class in
//! AngelScript.
template<typename T>
void bindEntityComponentBase(asIScriptEngine *engine, const char *type)
{
    // Forward declaration.
    int r;
    std::string sType = type;

    // Bind inherited functions.
    bindSerializableBase<T>(engine, type);

    // Set common behaviour.
    r = engine->RegisterObjectBehaviour("EntityComponent", asBEHAVE_REF_CAST, std::string(sType + "@ f()").c_str(),
                                        asFUNCTION((asRefCast<EntityComponent,T>)), asCALL_CDECL_OBJLAST); assert( r >= 0 );
    r = engine->RegisterObjectBehaviour(type, asBEHAVE_IMPLICIT_REF_CAST, std::string("EntityComponent@ f()").c_str(),
                                        asFUNCTION((asRefCast<T,EntityComponent>)), asCALL_CDECL_OBJLAST); assert( r >= 0 );

    // Bind common class functions.
    r = engine->RegisterObjectMethod(type, "u32 getID()",
                                     asMETHOD(T, getID), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod(type, "const string& getName()",
                                     asMETHOD(T, getName), asCALL_THISCALL); assert(r >= 0);

    r = engine->RegisterObjectMethod(type, "Entity@ getParent()",
                                     asMETHOD(T, getParent), asCALL_THISCALL); assert(r >= 0);
}

#endif // __COMPILE_WITH_ANGELSCRIPT__

#endif

